<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="600" destroy-on-close @closed="$emit('closed')">
		<el-container v-loading="loading" style="width: 100%;">
			<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px">
				<el-form-item label="门店名称" prop="name">
					<el-input v-model="form.shop_name" placeholder="门店名称" clearable></el-input>
				</el-form-item>
				<el-form-item label="所在地区" prop="region">
					<el-cascader :props="props" v-model="form.region" style="width: 100%;" clearable></el-cascader>
				</el-form-item>
				<el-form-item label="详细地址" prop="address">
					<el-input v-model="form.shop_address" placeholder="地址" clearable></el-input>
				</el-form-item>
				<el-form-item label="经纬度" prop="longitude">
					<el-input v-model="form.shop_longitude" placeholder="经度" clearable></el-input>
					<el-input v-model="form.shop_latitude" placeholder="维度" clearable></el-input>
				</el-form-item>
				<el-form-item label="联系方式" prop="shop_tel">
					<el-input v-model="form.shop_tel" placeholder="地址" clearable></el-input>
				</el-form-item>
				<el-form-item label="营业时间" prop="business_hours" v-if="false">
					<el-input v-model="form.business_hours" placeholder="地址" clearable></el-input>
				</el-form-item>
				<el-form-item label="排序" prop="sort" v-if="false">
					<el-input-number v-model="form.sort" controls-position="right" :min="1"
						style="width: 100%;"></el-input-number>
				</el-form-item>
				<el-form-item label="是否有效" prop="status" v-if="false">
					<el-switch v-model="form.status" :active-value="1" :inactive-value="0"></el-switch>
				</el-form-item>
				<el-form-item label="备注" prop="remark">
					<el-input v-model="form.remark" clearable type="textarea"></el-input>
				</el-form-item>
			</el-form>
		</el-container>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				loading: true,
				props: {
					lazy: true,
					lazyLoad: (node, resolve) => {
						const {
							level,
							value
						} = node;
						this.$API.system.region.all.get({
							level: level + 1,
							parent_id: value
						}).then(res => {
							const nodes = res.data.map((item) => {
								return ({
									value: item.id,
									label: item.name,
									leaf: item.level > 2
								})
							});
							resolve(nodes);
						});
					}
				},
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					id: "",
					address: "",
					business_hours: "",
					name: "",
					sort: 200,
					status: 1,
					remark: "",
					region: [],
				},
				//验证规则
				rules: {

					shop_address: [{
						required: true,
						message: '请输入地址'
					}],
					region: [{
						required: true,
						message: '请选择省市区'
					}],
					shop_name: [{
						required: true,
						message: '请输入门店名称'
					}],

				},
				//所需数据选项
				groups: [],
				groupsProps: {
					value: "id",
					label: "name",
					emitPath: false,
					checkStrictly: true
				}
			}
		},
		mounted() {

		},
		methods: {
			//显示
			open(mode = 'add') {
				this.mode = mode;
				if (this.mode == 'add') {
					this.loading = false;
				}
				this.visible = true;
				return this
			},
			//加载树数据
			async getGroup() {
				var res = await this.$API.system.dept.list.get();
				this.groups = res.data;
			},
			//表单提交方法
			submit() {
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						var res;
						this.isSaveing = true;
						if (this.mode == 'add') {
							res = await this.$API.system.shop.create.post(this.form);
						} else {
							res = await this.$API.system.shop.update.post(this.form);
						}
						this.isSaveing = false;
						if (res.code == 0) {
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						} else {
							this.$alert(res.message, "提示", {
								type: 'error'
							})
						}
					}
				})
			},
			//表单注入数据
			setData(data) {
				this.loading = true
				const params = {
					id: data.id
				}
				setTimeout(async () => {
					var res = await this.$API.system.shop.show.post(params)
					this.loading = false
					this.form = res.data
				}, 400)
			}
		}
	}
</script>

<style>
</style>
